//
//  ValueInput.swift
//  learn-ios
//
//  Created by Dong on 2022/4/4.
//

import SwiftUI

struct ValueInput: View {
    @State private var sliderValue: Float = 0.0
    @State private var stepperValue: Int = 0
    @State private var stepperColorValue: Int = 0
    @State private var toogleStatu: Bool = false

    var colors: [Color] = [.blue, .yellow, .gray, .red]

    var body: some View {
        VStack {
            Text("value: \(sliderValue)")
            Text("")

            // 滑动选择值
            // value: 绑定State属性
            // in: 设置值范围
            Slider(value: $sliderValue, in: 1 ... 10) {
                Text("value: \(sliderValue)")
            } minimumValueLabel: {
                Text("min: 1")
            } maximumValueLabel: {
                Text("max: 10")
            }

            // 累加&累减
            // value: 绑定State属性
            // step: 步长
            Stepper("value: \(stepperValue)", value: $stepperValue, step: 1)

            // 自定义加和减事件
            Stepper("value: \(stepperValue)") {
                stepperValue += 2
            } onDecrement: {
                stepperValue -= 1
            }

            // 示例: 根据下标选择颜色
            Stepper("value: \(stepperColorValue) blue: \(colors[stepperColorValue].description)", value: $stepperColorValue, step: 1) { _ in
                if stepperColorValue < 0 {
                    stepperColorValue = 3
                }
                if stepperColorValue > 3 {
                    stepperColorValue = 0
                }
            }

            // 切换控件
            Toggle("toogleStatu: \(toogleStatu.description)", isOn: $toogleStatu)
            Toggle(isOn: $toogleStatu) {
                Label("toogleStatu", systemImage: toogleStatu ? "star" : "star.fill")
            }
        }
        .padding()
    }
}

struct ValueInput_Previews: PreviewProvider {
    static var previews: some View {
        ValueInput()
    }
}
